package com.itggm.controller;

import com.itggm.Result.ResponseResult;
import com.itggm.Result.ResponseStatus;
import com.itggm.annotations.LogOperation;
import com.itggm.dto.*;
import com.itggm.emums.OperationType;
import com.itggm.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: ggm
 * @data 2024年12月18日 10:11
 */
@Slf4j
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("login")
    public ResponseResult login(@RequestBody UserLoginDTO userLoginDTO ,HttpServletRequest request) {
        log.info("开始登录{}", userLoginDTO);
        return userService.login(userLoginDTO,request);
    }

    /**
     * @Author: ggm
     * @date: 2024/12/20
     * 获取个人资料
     */
    @GetMapping("/personalInfo")
    public ResponseResult getPersonalInfo(HttpServletRequest request) {
        log.info("开始获取个人信息");
        String token = request.getHeader("authentication");
        return userService.getPersonalInfo(token);
    }

    /**
     * @Author: ggm
     * @date: 2024/12/20
     * 编辑个人信息
     */
    @PutMapping("/editPersonalInfo")
    public ResponseResult editPersonalInfo(@RequestBody EditPersonalInfoDTO editPersonalInfoDTO) {
        log.info("编辑个人信息{}", editPersonalInfoDTO);
        return userService.editPersonalInfo(editPersonalInfoDTO);

    }

    /**
     * @Author: ggm
     * @date: 2024/12/20
     * 修改密码
     */
    @PutMapping("/editPersonalPwd")
    public ResponseResult editPersonalPwd(HttpServletRequest request, @RequestBody EditPersonalPwdDTO editPersonalPwdDTO) {
        log.info("修改密码{}", editPersonalPwdDTO);
        String token = request.getHeader("authentication");
        return userService.editPersonalPwd(token, editPersonalPwdDTO);
    }

    /**
     * @Author: ggm
     * @date: 2024/12/20
     * 退出登录
     */
    @GetMapping("/logout")
    public ResponseResult logout(HttpServletRequest request) {
        log.info("退出登录");
        String token = request.getHeader("authentication");
        return userService.logout(token);
    }

    /**
     * @Author: ggm
     * @date: 2024/12/21
     * 分页查询
     */
    @LogOperation(model = "用户管理", operation = OperationType.SELECT)
    @PostMapping("/page")
    public ResponseResult page(@RequestBody UserPageDTO userPageDTO) {
        log.info("分页查询{}", userPageDTO);
        return userService.userPage(userPageDTO);
    }

    /**
     * @Author: ggm
     * @date: 2024/12/23
     * 添加用户
     */
    @LogOperation(model = "用户管理", operation = OperationType.ADD)
    @RequiresPermissions("sys:user:add")
    @PostMapping("/add")
    public ResponseResult addUser(@RequestBody UserDTO userDTO) {
        log.info("添加用户{}", userDTO);
        return userService.addUser(userDTO);
    }

    /**
     * @Author: ggm
     * @date: 2024/12/23
     * 编辑用户
     */
    @LogOperation(model = "用户管理", operation = OperationType.UPDATE)
    @RequiresPermissions("sys:user:edit")
    @PutMapping("/edit")
    public ResponseResult editUser(@RequestBody UserDTO userDTO) {
        log.info("编辑用户{}", userDTO);
        return userService.editUser(userDTO);
    }

    /**
     * @Author: ggm
     * @date: 2024/12/23
     * 批量删除
     */
    @LogOperation(model = "用户管理", operation = OperationType.DELETE)
    @RequiresPermissions("sys:user:delete")
    @DeleteMapping("delete/{ids}")
    public ResponseResult deleteUsers(@PathVariable String ids) {
        log.info("批量删除{}",ids);
        return userService.deleteUsers(ids);
    }

    /**
     * @Author: ggm
     * @date: 2024/12/23
     * 重置密码
     */
    @GetMapping("/resetPassword/{id}")
    public ResponseResult resetPassword(@PathVariable Integer id) {
        log.info("重置密码{}",id);
        return userService.resetPassword(id);

    }

    /**
     * @Author: ggm
     * @date: 2024/12/23
     * 修改用户状态
     */
    @RequiresPermissions("sys:user:state")
    @PutMapping("/changeUserState")
    public ResponseResult changeUserState(@RequestBody UserStateDTO userStateDTO) {
        log.info("修改用户状态{}",userStateDTO);
        return userService.changeUserState(userStateDTO);

    }
     /**
      * @Author: ggm
      * @date:  2024/12/25
      * 获取用户菜单权限
      */
     @GetMapping("/menu")
     public ResponseResult getUserMenu(HttpServletRequest request){
         log.info("获取用户菜单权限");
         String token = request.getHeader("authentication");
         return userService.getUserMenu(token);
     }
    @GetMapping("unAuth")
    public ResponseResult unAuth(){

        return  ResponseResult.fail()
                .code(ResponseStatus.UN_AUTH)
                .message(ResponseStatus.UN_AUTH.getMessage());
    }
}
